<?php

namespace App\Model;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * @property BelongsTo $projectLx
 * @property integer $seller_id
 * @property integer $ysid
 * @property integer $type
 * @property integer $id
 * @property integer $xmid
 * @property integer $create_role
 * @property mixed $role_list
 * @property mixed $approver
 * @property array $roles
 * @property array $uids
 * @property integer $work_id
 * @property float $offer
 * @property float $cost
 * @property float $total
 * @property float $tax
 * @property float $profit
 * @property float $percent
 * @property float $desc
 * @property integer $roleid
 * @property integer $deptid
 * @property integer $status
 * @property integer $node_id
 * @property float $sub_total
 * @property string $start
 * @property string $stop
 * @property string $created_at
 * @property integer $uid
 * @property integer $is_save
 * @property string $updated_at
 *
 */
class OAProjectYs extends OABaseModel
{
    protected $table = 'project_ys';
    public $timestamps = true;
    protected $appends = ['username'];
    protected $hidden = ['staff'];
    protected $fillable = [
        'xmid', 'uid', 'type', 'tax',
        'total', 'profit', 'percent', 'roleid',
        'desc', 'role_list', 'seller_id', 'is_save',
        'node_id', 'approver', 'create_role', 'created_at',
        'updated_at', 'work_id', 'status', 'offer', 'cost',
        'ysid','deptid','sub_total','start','stop'
    ];
    protected $attributes = ['approver' => 'array','role_list'=>"array"];
    protected $casts = ['approver' => 'array','role_list'=>"array"];
    const PROJECT_YS = 1;
    const PROJECT_JS = 2;

    //预算详情
    public function ysDetail()
    {
        return $this->hasMany(OAProjectYsDetail::class, 'ysid');
    }

    //删除预算数据
    public static function delYsData($params)
    {
        if (isset($params['xmid'])) return self::query()->where('xmid', $params['xmid'])->delete();

        return self::query()->where('id', $params['id'])->delete();

    }

    public function projectLx(): BelongsTo
    {
        return $this->belongsTo(OAProjectLx::class, 'xmid');
    }

    public function getTaxAttribute($tax)
    {
        return number_format($tax, 2);
    }

    public function getTotalAttribute($total)
    {
        return number_format($total, 2);
    }

    public function getProfitAttribute($profit)
    {
        return number_format($profit, 2);
    }

    public function staff()
    {
        return $this->belongsTo(OAStaff::class, 'uid');
    }

    public function getUsernameAttribute()
    {
        return optional($this->staff)->username ?? '-';
    }

    public function getRolesAttribute()
    {
        $roles = $this->role_list;
        return OAPositionMapping::getPositionMapping($roles);
    }

    public function getUidsAttribute()
    {
        $arr =  $this->approver;
        $arr[] = $this->seller_id;
        $arr[] = $this->uid;
        $arr = array_filter($arr);
        return $arr;
    }

    public function getOfferAttribute($offer)
    {
        return fomateMoney("", $offer);
    }

    public function getCostAttribute($cost)
    {
        return fomateMoney("", $cost);
    }

    public function work()
    {
        return $this->belongsTo(OAWork::class, 'work_id');
    }

    public function getSubTotalAttribute($sub_total)
    {
        if($sub_total > 0) return fomateMoney("", $sub_total);
        $sub_total = bcsub(resetMoney($this->total), resetMoney($this->tax),2);
        return fomateMoney("", $sub_total);
    }

    public function getStartAttribute($start)
    {
        if($start) return Carbon::parse($start)->format('Y-m-d');
        $lxStart = optional($this->projectLx)->start ?? '';
        if ($lxStart) return Carbon::parse($lxStart)->format('Y-m-d');
        return Carbon::parse($this->created_at)->format('Y-m-d');
    }

    public function getStopAttribute($stop)
    {
        if($stop) return Carbon::parse($stop)->format('Y-m-d');
        $lxStop = optional($this->projectLx)->stop ?? '';
        if ($lxStop) return Carbon::parse($lxStop)->format('Y-m-d');
        return Carbon::parse($this->updated_at)->format('Y-m-d');
    }


}
